package com.zyn.rsgl.controller;

import com.zyn.rsgl.pojo.Schedule;
import com.zyn.rsgl.pojo.ScheduleDetail;
import com.zyn.rsgl.service.ScheduleDetailService;
import com.zyn.rsgl.service.ScheduleService;
import com.zyn.rsgl.utils.commonResult.R;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 排班管理 前端控制器
 * </p>
 *
 * @author zyn
 * @since 2025-03-03
 */

@RestController
@RequestMapping("/rsgl/schedule")
public class ScheduleController {
    @Resource
    private ScheduleService scheduleService;

    @Resource
    private ScheduleDetailService scheduleDetailService;

    // 查询排班模板分页列表
    @GetMapping("/list")
    public R listSchedules(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           @RequestParam(required = false) String name,
                           @RequestParam(required = false) String description) {
        return scheduleService.listSchedulesPage(pageNum, pageSize, name, description);
    }

    // 根据ID获取排班模板详细信息
    @GetMapping("/get/{scheduleId}")
    public R getSchedule(@PathVariable("scheduleId") String scheduleId) {
        Schedule schedule = scheduleService.getById(scheduleId);
        if (schedule != null) {
            return R.ok(schedule);
        }
        return R.fail("未查询到该排班模板");
    }

    // 添加排班模板
    @PostMapping("/add")
    public R addSchedule(@RequestBody Schedule schedule) {
        if (scheduleService.getById(schedule.getId()) != null) {
            return R.fail("该排班模板已存在");
        }
        return scheduleService.save(schedule) ? R.ok() : R.fail("添加失败");
    }

    // 修改排班模板
    @PutMapping("/update")
    public R updateSchedule(@RequestBody Schedule schedule) {
        return scheduleService.updateById(schedule) ? R.ok() : R.fail("修改失败");
    }

    // 删除排班模板
    @DeleteMapping("/delete/{scheduleId}")
    public R deleteSchedule(@PathVariable("scheduleId") String scheduleId) {
        return scheduleService.removeById(scheduleId) ? R.ok() : R.fail("删除失败");
    }

    // 查询排班详情分页列表
    @GetMapping("/details/list")
    public R listDetails(@RequestParam String scheduleId,
                         @RequestParam(defaultValue = "1") Integer pageNum,
                         @RequestParam(defaultValue = "10") Integer pageSize,
                         @RequestParam(required = false) String dayOfWeek,
                         @RequestParam(required = false) String startTime,
                         @RequestParam(required = false) String endTime) {
        return scheduleService.listDetailsPage(scheduleId, pageNum, pageSize, dayOfWeek, startTime, endTime);
    }

    // 添加排班详情
    @PostMapping("/details/add")
    public R addDetail(@RequestBody ScheduleDetail detail) {
        if (scheduleService.detailExists(detail)) {
            return R.fail("该排班详情已存在");
        }
        return scheduleDetailService.save(detail) ? R.ok("添加成功") : R.fail("添加失败");
    }

    // 修改排班详情
    @PutMapping("/details/update")
    public R updateDetail(@RequestBody ScheduleDetail detail) {
        return scheduleDetailService.updateById(detail) ? R.ok() : R.fail("修改失败");
    }

    // 删除排班详情
    @DeleteMapping("/details/delete/{detailId}")
    public R deleteDetail(@PathVariable("detailId") String detailId) {
        return scheduleDetailService.removeById(detailId) ? R.ok() : R.fail("删除失败");
    }

    // 查询用户排班信息
    @GetMapping("/user/{userId}")
    public R getUserSchedule(@PathVariable("userId") String userId, @RequestParam String date) {
        return scheduleDetailService.getUserSchedule(userId, date);
    }
}
